﻿using System;
using System.Collections.Generic;
using System.Text;
using FlickrNet;
using System.Configuration;
using McePicasa.Properties;

namespace McePicasa.mceFlickr
{
    class FlickrAPI
    {
        private Flickr flickr;
        public FlickrAPI()
        {
            //If the user is still authenticated then automatically auth. him
            if (Settings.Default.UserToken != "")
            {
                this.flickr = new Flickr(Settings.Default.flickrApiKey, Settings.Default.SharedSecret, Settings.Default.UserToken);
            }
            else
            {
                this.flickr = new Flickr(Settings.Default.flickrApiKey, Settings.Default.SharedSecret);
            }
        }


        /// <summary>
        /// Function to get user's public gallery 
        /// </summary>
        /// <param name="UserName">Name of the user to search his photos</param>
        /// <param name="ResultsPageNumber">Wich page to show</param>
        /// <returns></returns>
        public Photos SearchUserPublicCollection(string UserName, int ResultsPageNumber)
        {
            PhotoSearchOptions pso = new PhotoSearchOptions();
            FoundUser user = null;
            try
            {
                user = flickr.PeopleFindByUsername(UserName);
            }
            catch (FlickrApiException fe)
            {
                Application.Current.MessageBox("Error during user lookup. It look's like there's no such user.");
                Log.LogIt(fe);
            }
            if (user != null)
            {
                pso.UserId = user.UserId;
                pso.Page = ResultsPageNumber;
                Photos photos = this.flickr.PhotosSearch(pso);
                return photos;
            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// Method to search for photos by some tag words
        /// </summary>
        /// <param name="tag">Tag words</param>
        /// <param name="pageNumber">The number of page to display</param>
        /// <returns>Returns found phots collection</returns>
        public FlickrAlbum SearchPhotosByTag(string tag, long pageNumber)
        {
            PhotoSearchOptions pso = new PhotoSearchOptions();
            //Application.Current.MessageBox(tag, "Tagi");
            pso.Tags = tag;
            pso.Page = (int)pageNumber;
            pso.PerPage = 20;
            Photos photos = flickr.PhotosSearch(pso);

            FlickrAlbum album = new FlickrAlbum();

            album.Photos = photos;


            return album;


        }
        /// <summary>
        /// Extract single photo from Photos collection based on his photoId, usefull for browsing photos
        /// </summary>
        /// <param name="photosCollection">Collection of photos to search</param>
        /// <param name="photoId">The photo to search for</param>
        /// <returns>Single photo object</returns>
        public Photo GetPhotoInformation(Photos photosCollection, string photoId)
        {
            foreach (Photo item in photosCollection.PhotoCollection)
            {
                if (item.PhotoId == photoId)
                {
                    return item;
                }
            }
            return null;
        }
        /// <summary>
        /// Gets conctact collection of authorized user
        /// </summary>
        /// <returns>Returns contact collection</returns>
        public Contacts GetUserContacts()
        {
            Contacts contacts = null;

            try
            {
                contacts = this.flickr.ContactsGetList();
            }
            catch (FlickrException fe)
            {
                Application.Current.MessageBox("Sorry there was an error during reading Your contacts.", "Flickr error");
                Log.LogIt(fe);
            }
            return contacts;

        }
        /// <summary>
        /// Returns user found by his name
        /// </summary>
        /// <param name="UserName">Screen name of user to find</param>
        /// <returns>Object containing found user details</returns>
        public FoundUser FindUserByUsername(string UserName)
        {
            FoundUser contact = null;
            try
            {
                contact = this.flickr.PeopleFindByUsername(UserName);
            }
            catch (FlickrException fe)
            {
                Application.Current.MessageBox("Sorry there was error during user lookup", "Flickr error");
                Log.LogIt(fe);
            }
            return contact;

        }



        /// <summary>
        /// This method takes user name and returns Flickr photosets for this user.
        /// </summary>
        /// <param name="UserName">User name</param>
        /// <returns>Collection of photosets</returns>
        public Photosets GetUsersPhotosets(string UserName)
        {
            Photosets ps = null;

            try
            {
                FoundUser fu = this.flickr.PeopleFindByUsername(UserName);
                ps = flickr.PhotosetsGetList(fu.UserId);

                return ps;
            }
            catch (FlickrException fe)
            {
                Application.Current.MessageBox("Sorry but we couldn't find any photoset for this user name.", "No photoset found");
                Log.LogIt(fe);
                return null;
            }
        }

        /// <summary>
        /// Method for fetching photos of current collection
        /// </summary>
        /// <param name="photosetId"></param>
        /// <returns></returns>
        public Photoset GetPhotosetPhotos(string photosetId)
        {
            Photoset ps = null;
            try
            {
                ps = flickr.PhotosetsGetPhotos(photosetId);
                return ps;
            }
            catch (FlickrException fe)
            {
                Application.Current.MessageBox("Sorry but there was an error during photo fetching.", "Photo not fetched");
                Log.LogIt(fe);
                return null;
            }
        }

        public bool CheckIfUserExists(string UserName)
        {
            FoundUser fu = null;

            try 
	        {
                fu = this.flickr.PeopleFindByUsername(UserName);
                return true;
	        }
	        catch (FlickrException fe)
	        {
                //Application.Current.MessageBox("Sorry user not found", "User not foud");                
                Log.LogIt(fe);
                return false;
	        }
        }
    }
}
